package cn.lvxs.freeeast.base_System;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

import com.wabacus.config.ConfigLoadManager;
import com.wabacus.config.component.application.report.ReportBean;
import com.wabacus.system.ReportRequest;
import com.wabacus.system.intercept.AbsInterceptorDefaultAdapter;
import com.wabacus.util.DesEncryptTools;

public class Interceptor_SysMenuSave extends AbsInterceptorDefaultAdapter
{
    public int beforeSavePerRow(ReportRequest rrequest,ReportBean rbean,Map mRowData,Map mExternalValues,int updatetype)
    {
        if(updatetype==WX_INSERT)
        {//对本条记录做添加操作
        }else   if(updatetype==WX_UPDATE)
        {//对本条记录做修改操作
        }else if(updatetype==WX_DELETE)
        {//对本条记录做删除操作
        	if(mRowData!=null)//mRowData中存放了本条记录各列的值
            {
        		String MenuID_old=(String)mRowData.get("MenuID_old");
        		String ID=(String)mRowData.get("ID");
        		String MenuID="";
        		String parentMenuName_old=(String)mRowData.get("parentMenuName_old");
        		String parentMenuID=parentMenuName_old;
        		try {
    				Connection conn = rrequest.getConnection();
    				PreparedStatement stmt = null;
    				//从数据库中取得用户信息，进行判断
    				try {
    					if(parentMenuName_old!=null && !parentMenuName_old.equalsIgnoreCase("-1")){
    						stmt = conn.prepareStatement("select * from SysMenu where ID=?");
    						stmt.setString(1, MenuID_old);
    						ResultSet rs = stmt.executeQuery();
    						if (rs.next()) {
    							MenuID=rs.getString("MenuID");
    							parentMenuID=rs.getString("ID");
    						}
							stmt.close();
    					}
    					if(MenuID_old!=null && !MenuID_old.isEmpty()){
							stmt = conn.prepareStatement("Update SysMenu set MenuID=concat_ws('',?,SUBSTRING(MenuID,length(?)+1)) where MenuID like concat_ws('',?,'%') and (ParentID =? or ParentID in (select ID from SysMenu where ParentID =?) or length(MenuID)>?)");
							stmt.setString(1, MenuID);
							stmt.setString(2, MenuID_old);
							stmt.setString(3, MenuID_old);
							stmt.setString(4, parentMenuID);
							stmt.setString(5, parentMenuID);
							stmt.setLong(6, MenuID_old.length()+6);
							stmt.executeUpdate();
							stmt.close();
    					}
						stmt = conn.prepareStatement("Update SysMenu set ParentID=? where ParentID =?");
						stmt.setString(1, parentMenuID);
						stmt.setString(2, ID);
						stmt.executeUpdate();
    				} catch (SQLException e) {
    				} finally {
    					try {
    						if (stmt != null) {
    							stmt.close();
    						}
    					} catch (SQLException e) {
    					}
    				}
        		} catch (Exception e) {
        		}
            }
        }
        return WX_CONTINUE;
    }

    public int afterSavePerRow(ReportRequest rrequest,ReportBean rbean,Map mRowData,Map mExternalValues,int updatetype)
    {
        if(updatetype==WX_INSERT)
        {//对本条记录做添加操作
        }else   if(updatetype==WX_UPDATE)
        {//对本条记录做修改操作
        	if(mRowData!=null)//mRowData中存放了本条记录各列的值
            {
        		String MenuID_old=(String)mRowData.get("MenuID_old");
        		String MenuID=(String)mRowData.get("MenuID");
        		String parentMenuID_old=(String)mRowData.get("parentMenuName_old");
        		String parentMenuID=(String)mRowData.get("parentMenuName");
        		if(MenuID_old!=null && !MenuID_old.isEmpty() && !MenuID_old.equalsIgnoreCase(MenuID)){
            		try {
        				Connection conn = rrequest.getConnection();
        				PreparedStatement stmt = null;
        				//从数据库中取得用户信息，进行判断
        				try {
							stmt = conn.prepareStatement("Update SysMenu set MenuID=concat_ws('',?,SUBSTRING(MenuID,length(?)+1)) where MenuID like concat_ws('',?,'%') and (ParentID =? or ParentID in (select ID from SysMenu where ParentID =?) or length(MenuID)>?)");
							stmt.setString(1, MenuID);
							stmt.setString(2, MenuID_old);
							stmt.setString(3, MenuID_old);
							stmt.setString(4, parentMenuID_old);
							stmt.setString(5, parentMenuID_old);
							stmt.setLong(6, MenuID_old.length()+6);
							stmt.executeUpdate();
        				} catch (SQLException e) {
        				} finally {
        					try {
        						if (stmt != null) {
        							stmt.close();
        						}
        					} catch (SQLException e) {
        					}
        				}
            		} catch (Exception e) {
            		}
        		}
            }
        }else if(updatetype==WX_DELETE)
        {//对本条记录做删除操作
        }
        return WX_CONTINUE;
    }
}
